package com.ciwong.epaper.modules.evaluate.ui;

import android.view.Gravity;
import android.widget.Toast;

import com.ciwong.epaper.widget.evaluate.SentenceSpeechView;

/**
 * Created by lzr on 2017/4/17.
 * 播放全文
 */
public class SpeechAllPlayController extends SpeechPlayController {

    private MyErNeThread myErNeThread;

    private boolean isNotErr = true;

    public SpeechAllPlayController(SentenceSpeechView speechView) {
        this.mContext = speechView.getContext();
        this.speechView = speechView;
    }

    @Override
    protected void stopOrStartPlay(Object mp3) {
        if (isNotErr) {
            playNext();
        }
    }

    @Override
    protected void playNext() {
        isNotErr = true;
        currentListenAndRepeatIndex++;
        if (currentListenAndRepeatIndex < (sentences.size())) {
            //如果当前播放的下标还在句子列表中就继续播放下一个句子
            play();
            if (speechPlayInterFace != null) {
                speechPlayInterFace.nextPlay(currentListenAndRepeatIndex, sentences.size(), false);
            }
        } else {
            if (speechPlayInterFace != null) {
                speechPlayInterFace.nextPlay(currentListenAndRepeatIndex, sentences.size(), false);
            }
            currentListenAndRepeatIndex = 0;
        }
    }

    @Override
    protected void playError(Object tag) {
        if (!"pcm".equals(tag + "")) {
            //设置为10秒
            audioDuration = 10000;
            isNotErr = false;
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    speechView.closeStateIm(mIsUnAutoMode);
                    Toast toast = Toast.makeText(mContext, "播放音频失败", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                    toast.show();
                }
            });
            myErNeThread = new MyErNeThread();
            mHandler.postDelayed(myErNeThread, 2000);
        }
    }


    @Override
    public void refreshSpeechView(boolean isPlayState) {
        super.refreshSpeechView(false);
    }

    class MyErNeThread extends Thread {

        @Override
        public void run() {
            playNext();
        }
    }

}

